import logging

# App Engine imports
from google.appengine.ext import webapp
from google.appengine.api import users

from dao import ContactLogDao

class AdminServer(webapp.RequestHandler):
  """Handles requests to /admin URLs and delegates to the Admin class."""

  def test_rsa(self):
    import verify
    return verify.verify()

  def _handleRequest(self):
    action = self.request.get('action')
    if action == 'list':
      dao = ContactLogDao()
      self.response.out.write("Contact logs = %s" % dao.get_log_list())
    elif action == 'test_rsa':
      if self.test_rsa():
        self.response.out.write("Signature verified")
      else:
        self.response.out.write("Invalid signature")                
    else:
      html = []
      html.append('<a href="/admin?action=list">List all data in datastore</a>')
      self.response.out.write(''.join(html))

  def get(self):
    """Ensure that the user is an admin, then invoke appropriate action."""
    if not users.GetCurrentUser():
      loginUrl = users.CreateLoginURL(self.request.uri)
      self.response.out.write('<a href="%s">Login</a>' % loginUrl)
      return

    if not users.IsCurrentUserAdmin():
      self.response.out.write('You must be an admin to view this page.')
      return

    self._handleRequest()
